package com.okilo.online.app.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import com.okilo.online.app.R
import com.okilo.online.app.common.OkiloComDlog
import com.okilo.online.app.databinding.OkiloDlogAppVersionUpdateBinding
import com.okilo.online.app.databinding.OkiloDlogAppVersionUpdateReasonItemBinding
import com.okilo.online.app.event.OkiloEventPointType
import com.okilo.online.app.tools.helper.OkiloConfigCustomerHp

/**
 * OkiloLoan
 * @Author wwdeng
 * @CreateTime: 2024/11/13 15:41
 * @Annotation：提示dialog
 */
class OkiloUpdateDlog: OkiloComDlog {

    private lateinit var binding: OkiloDlogAppVersionUpdateBinding
    private var mCallback: IDialogCallback? = null

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context, remark: String) : super(context) {
        dialogRemark = remark
    }
    constructor(context: Context, remark: String, callback: IDialogCallback?) : super(context) {
        mCallback = callback
        dialogRemark = remark
    }

    override val dialogName: String = "Update_Version"

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun createOkiloContentView(): View {
        binding = OkiloDlogAppVersionUpdateBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    @SuppressLint("SetTextI18n")
    override fun initializeOkiloViews() {
        val releaseData = OkiloConfigCustomerHp.gOkiloReleaseObject()
        releaseData?.let {
            binding.okiloAppVersionUpdateCodeText.text = "V${it.optString("versionName")}"
            //添加更新描述
            addOkiloVersionUpdateReason(it.optString("versionRemark"))

            //是否强制更新
            val isMustUpdate = OkiloConfigCustomerHp.iOkiloReleaseMustUpdate()
            if (isMustUpdate){
                binding.okiloAppVersionUpdateWaitLayout.visibility = View.GONE
                binding.okiloAppVersionUpdateCloseIcon.visibility = View.GONE
                setCancelable(false)
                setCanceledOnTouchOutside(false)
            } else {
                binding.okiloAppVersionUpdateWaitLayout.visibility = View.VISIBLE
                binding.okiloAppVersionUpdateCloseIcon.visibility = View.VISIBLE
                setCancelable(true)
                setCanceledOnTouchOutside(true)
            }
        }
    }

    override fun setupOkiloEventListeners() {
        setOnClickListener(binding.okiloAppVersionUpdateCloseIcon) //关闭
        setOnClickListener(binding.okiloAppVersionUpdateButtonNow) //立即更新
    }

    override fun loadOkiloData() {
    }

    override fun onOkiloEventViewClick(v: View?) {
        when(v?.id){
            R.id.okiloAppVersionUpdateCloseIcon -> {
                addEventPointRequestAction(OkiloEventPointType.OKILO_CLICK_TRACK.UPDATE_VERSION_CLOSE,dialogRemark)
            }
            R.id.okiloAppVersionUpdateButtonNow -> {
                addEventPointRequestAction(OkiloEventPointType.OKILO_CLICK_TRACK.UPDATE_VERSION_NOW,dialogRemark)
                mCallback?.onUpdateClick()
            }
        }
        dismiss()
    }

    /**
     * 添加更新描述
     */
    private fun addOkiloVersionUpdateReason(remark: String){
        binding.okiloAppVersionUpdateReasonLayout.removeAllViews()
        if (!remark.contains("$")){
            val itemBinding = OkiloDlogAppVersionUpdateReasonItemBinding.inflate(layoutInflater)
            itemBinding.okiloAppVersionUpdateReasonText.text = remark
            binding.okiloAppVersionUpdateReasonLayout.addView(itemBinding.root)
            return
        }
        val list = remark.split("$")
        list.forEach {
            val itemBinding = OkiloDlogAppVersionUpdateReasonItemBinding.inflate(layoutInflater)
            itemBinding.okiloAppVersionUpdateReasonText.text = it
            binding.okiloAppVersionUpdateReasonLayout.addView(itemBinding.root)
        }
    }

    interface IDialogCallback {
        //立即更新
        fun onUpdateClick()
    }
}